package com.yeyuanqi.hottop.controller;

import com.yeyuanqi.hottop.common.Result;
import com.yeyuanqi.hottop.service.RedisService;
import com.yeyuanqi.hottop.untils.DailyAndMonthlyTopDataUtil;
import com.yeyuanqi.hottop.vo.NameAndValueVO;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * 每日热度top10控制器
 * @author yeyuanqi
 * @since 2022-01-19
 */
@RestController
@RequestMapping("/dailytop")
public class DailyTopController {

    @Autowired
    private RedisService redisService;
    
    @Autowired
    private DailyAndMonthlyTopDataUtil dailyAndMonthlyTopDataUtil;

    /**
     * 某天全部热搜的top10
     * @return 可排序柱状图数据格式
     */
    @GetMapping("/{sitename}/{day}")
    public Result selectDailyTop(@PathVariable("sitename") String sitename,@PathVariable("day") String day){
        String rediskeyname;
        switch (sitename){
            case "bdhot":
                rediskeyname = day + "-baidudailytop";break;
            case "jrhot":
                rediskeyname = day + "-jinridailytop";break;
            case "txhot":
                rediskeyname = day + "-tengxundailytop";break;
            case "wbhot":
                rediskeyname = day + "-weibodailytop";break;
            default:
                return Result.fail("没有该网站的热搜信息");
        }
        Map<Object, Double> dailytopdata = redisService.zReverseRangeWithAllScores(rediskeyname);
        List<NameAndValueVO> dailyAndMonthlyHotVOS = dailyAndMonthlyTopDataUtil.getdailyTopDatas(dailytopdata);
        if (0 != dailyAndMonthlyHotVOS.size()){
            return Result.succ(dailyAndMonthlyHotVOS);
        } else {
            return Result.fail("该天没有热搜top信息");
        }
    }

}
